﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using FaceSharp.Api.GraphApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Types = FaceSharp.Api.Types;

namespace FaceSharp.Api.Objects
{
    public class Album : GraphBase
    {
        private readonly User _user;
        private readonly TypedGraphClient<Album> _albumGraphClient;

        public Album(IFacebookCore facebookCore)
            : base(facebookCore)
        {
            _user = new User(facebookCore);
        }
        
        public Types.Album Get(string albumId)
        {
            Trace.Write("Begin Request :" + albumId, "Facebook.Objects.Album.Get");
            var jObject = MakeGraphRequest(albumId);
            var album = new Types.Album();
            if (jObject != null)
            {
                album = new Types.Album()
                {
                    Aid = (string)jObject["id"],
                    Name = (string)jObject["name"]
                };
            }
            Trace.Write("End Request", "Facebook.Objects.Album.Get");
            return album;
        }

        public Types.Album Add(string name, string message)
        {
            Trace.Write("Begin Request", "Facebook.Objects.Album.Add");
            var jObject = MakeGraphPost("me", "albums", new Dictionary<string, string>()
                                                            {
                                                                {"name", name},
                                                                {"message", message}
                                                            });

            var album = new Types.Album();
            if (jObject != null)
            {
                album = Get((string)jObject["id"]);
                var cache = HttpRuntime.Cache;
                cache.Remove("Facebook.Objects.Album.List_" + _user.GetCurrentUserId());
            }
            Trace.Write("End Request", "Facebook.Objects.Album.Add");
            return album;
        }
        private long ApiMergeAdd(long aid,long uid)
        {
            return (uid << 32) + (aid + 0xFFFFFFFF);
        }

        public IEnumerable<Types.Album> List(bool noCache = false)
        {
            var cacheKey = "Facebook.Objects.Album.List_" + _user.GetCurrentUserId();
            var cache = HttpRuntime.Cache;
            var albums = (List<Types.Album>)cache[cacheKey];
            if (albums == null || noCache == true)
            {
                // get profile albumn id : http://forum.developers.facebook.net/viewtopic.php?pid=179219
                //var profileAlbumId = ApiMergeAdd(-3, _user.GetCurrentUserId());

                var jObject = MakeGraphRequest("me", "albums");
                if (jObject != null)
                {
                    var jData = jObject["data"];
                    if(jData != null) albums = jData.Select(x => x.ToObject<Types.Album>()).ToList();
                    if(albums != null) cache.Insert(cacheKey, albums, null, DateTime.Now.AddMinutes(50), TimeSpan.Zero);
                }
            }
            return albums;
        }

        public IEnumerable<Types.Photo> GetPhotos(string albumId, bool noCache = false)
        {
            Trace.Write("Begin Request - Album ID:" + albumId, "Facebook.Objects.Album.GetPhotos");
            var cacheKey = "Facebook.Objects.Album.GetPhotos_" + albumId;
            var cache = HttpRuntime.Cache;
            var photos = (List<Types.Photo>)cache[cacheKey];

            if (photos == null || noCache)
            {
                var jObject = MakeGraphRequest(albumId, "photos");
                if (jObject != null)
                {
                    var jData = (JArray)jObject["data"];
                    if (jData != null) photos = jData.Select(x => x.ToObject<Types.Photo>()).ToList();
                }
                if (photos != null) cache.Insert(cacheKey, photos, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
            }
            Trace.Write("End Request", "Facebook.Objects.Album.GetPhotos");
            return photos;
        }
    }
}